import { getSourceAuth, getSourceRole } from '@/api/source'
import type { RoleRes } from '@/types/role'

import explorerStore from '@/stores/modules/explorer'

/*
 * @Author       : Hejh(3010733382@qq.com)
 * @Version      : V1.0
 * @Date         : 2024-05-17 16:00:54
 * @Description  : 获取用户文档权限
 */

export const useSourceAuth = () => {
  const sourceAuths = ref<string[]>([])
  const sourceAuth = ref<any>(null)
  const loadSourceAuth = (sourceIds: string[], delay = 300) => {
    clearTimeout(sourceAuth.value)
    sourceAuth.value = setTimeout(() => {
      getSourceAuth({
        sourceIds
      })
        .then((res) => {
          // console.log(res.data)
          sourceAuths.value = res.data
        })
        .catch(console.error)
        .finally(() => {})
    }, delay)
  }

  onBeforeMount(() => {
    clearTimeout(sourceAuth.value)
  })

  return {
    sourceAuths,
    loadSourceAuth
  }
}

export const useSourceRole = () => {
  const sourceRole = ref<any>({
    id: '-1',
    name: '无权限',
    ename: 'noAuth',
    label: '#f7f7f7'
  })
  const sourceRoleTimer = ref<any>(null)
  const loadSourceRole = (sourceId: string, delay = 300) => {
    clearTimeout(sourceRoleTimer.value)
    sourceRoleTimer.value = setTimeout(() => {
      getSourceRole({
        sourceId
      })
        .then((res) => {
          // console.log(res.data)
          sourceRole.value = res.data
          explorerStore().curSourceDesc!.sourceRole = res.data
        })
        .catch(console.error)
        .finally(() => {})
    }, delay)
  }

  onBeforeMount(() => {
    clearTimeout(sourceRoleTimer.value)
  })

  return {
    sourceRole,
    loadSourceRole
  }
}
